﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ABB.Common.DataModel;
using System.Data;

namespace ABB.Common.BusinessLogic.Reporting
{
    /// <summary>
    /// Erstellt aus einer Report Definition eine DataTable mit entsprechendem Schema.
    /// </summary>
    internal class ReportDataTableSchemaBuilder
    {
        private readonly ProjectPlanningDataSet m_DataSet;
        private readonly ProjectPlanningDataSet.ReportRow m_Report;

        /// <summary>
        /// Öffentlicher Konstruktor, initialisiert den Builder mit der
        /// entsprechende <see cref="ProjectPlanningDataSet.ReportRow">Report-Definition</see>.
        /// </summary>
        /// <param name="dataSet"></param>
        /// <param name="report"></param>
        public ReportDataTableSchemaBuilder(ProjectPlanningDataSet dataSet,
                                            ProjectPlanningDataSet.ReportRow report)
        {
            if (dataSet == null) throw new ArgumentNullException("dataSet");
            if (report == null) throw new ArgumentNullException("report");

            this.m_DataSet = dataSet;
            this.m_Report = report;
        }

        /// <summary>
        /// Baut eine DataTable mit dem Schema des Reports, mit welchem
        /// der ReportDataTableSchemaBuilder initialisiert wurde.
        /// </summary>
        /// <returns></returns>
        public DataTable BuildDataTable(out ReportColumnMapping mappingDictionary)
        {
            DataTable table = new DataTable(this.m_Report.Name);
            mappingDictionary = new ReportColumnMapping();

            foreach (var column in this.m_Report.GetReportColumnRows().Where(r => r.IsVisible)
                                                                    .OrderBy(r => r.SortIndex))
            {
                DataColumn dataColumn = table.Columns.Add(column.GetColumnName(), typeof(string));
                mappingDictionary.Add(dataColumn, column);
            }

            return table;
        }
    }
}
